package com.sunday.aomicaichang.ui.order;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.imageselector.MultiImageSelectorActivity;
import com.sunday.common.utils.ImageUtils;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.adpter.OrderAssessAdapter;
import com.sunday.aomicaichang.model.OrderAssess;
import com.sunday.member.entity.order.OrderItem;
import com.sunday.member.entity.order.OrderProduct;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/6/11.
 */

public class OrderAssessActivity extends BaseActivity {
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.btn_commit)
    TextView btnCommit;

    private OrderItem order;
    private OrderAssessAdapter assessAdapter;
    private LinearLayoutManager linearLayoutManager;
    private List<OrderAssess> orderAssessList=new ArrayList<>();
    private ArrayList<String> mSelectPath=new ArrayList<>() ;
    public int position;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_assess);
        ButterKnife.bind(this);
        order = (OrderItem) getIntent().getSerializableExtra("order");
        for(OrderProduct product:order.getProducts()){
            OrderAssess orderAssess=new OrderAssess();
            orderAssess.setId(product.getId());
            orderAssess.setName(product.getName());
            orderAssess.setImg(product.getImage());
            List<Object> ids=new ArrayList<>();
            ids.add(R.mipmap.add);
            orderAssess.setUrl(ids);
            orderAssessList.add(orderAssess);
        }
        linearLayoutManager=new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(linearLayoutManager);
        assessAdapter=new OrderAssessAdapter(mContext,this,orderAssessList,mSelectPath);
        recyclerView.setAdapter(assessAdapter);
//        assessAdapter.setOnItemClickListener(new OrderAssessAdapter.OnRecyclerViewItemClickListener() {
//
//            @Override
//            public void onItemClick(View view, int position) {
//            }
//        });
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            position=data.getIntExtra("position",0);
            showLoadingDialog(0);
            handleImages();
    }
    public void setPosition(int position){
        this.position=position;
    }
    private void showImages(){
        if(orderAssessList.get(position).getUrl()!=null)
        orderAssessList.get(position).getUrl().clear();
        orderAssessList.get(position).getUrl().addAll(mSelectPath);
        orderAssessList.get(position).getUrl().add(R.mipmap.add);
        assessAdapter.notifyDataSetChanged();
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            showImages();
            dissMissDialog();
        }
    };

    private void handleImages() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < mSelectPath.size(); i++) {
                    mSelectPath.set(i, ImageUtils.getCompressImagePath(mSelectPath.get(i)));
                }
                handler.sendEmptyMessage(0);

            }
        }).start();
    }

}
